package licode.unisop.client.service.manager;

import licode.unisop.client.api.SopPlatform;
import licode.unisop.client.config.YmlPlatform;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 管理平台和认证中心的交互，包括验证平台是否合法，
 * 只有平台验证后，后续登录认证操作才能继续完成
 *
 * @author WR
 */
@Data
@Slf4j
@Service
class SopPlatformLocal {
    private final Map<String, SopPlatform> pTokens = new ConcurrentHashMap<>();
    private final Map<String, SopPlatform> platforms = new ConcurrentHashMap<>();
    private final Map<String, YmlPlatform.Register> plInfos = new ConcurrentHashMap<>();

    private SopPlatform defPlatform;

    public SopPlatform getPlatform(String platId) {
        if (null == platId || platId.isEmpty() || "default".equals(platId)) {
            return defPlatform;
        } else {
            return platforms.get(platId);
        }
    }

    public SopPlatform getPlatformByToken(String token) {
        return pTokens.get(token);
    }

    public SopPlatform def() {
        return defPlatform;
    }

    public List<String> getPlatformIdList() {
        return new ArrayList<>(platforms.keySet());
    }
}
